home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / misc / gnuplot-3.7src.lha / gnuplot-3.7src / gnuplot-3.7.lha / gnuplot-3.7 / docs / doc2rtf.c < prev    next >
C/C++ Source or Header  |  1998-10-19  |  8KB  |  310 lines

  1. /*
  2.  * $Id: doc2rtf.c,v 1.16 1998/04/14 00:17:00 drd Exp $
  3.  *
  4.  */
  5.  
  6. /* GNUPLOT - doc2rtf.c */
  7.  
  8. /*[
  9.  * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
  10.  *
  11.  * Permission to use, copy, and distribute this software and its
  12.  * documentation for any purpose with or without fee is hereby granted,
  13.  * provided that the above copyright notice appear in all copies and
  14.  * that both that copyright notice and this permission notice appear
  15.  * in supporting documentation.
  16.  *
  17.  * Permission to modify the software is granted, but not the right to
  18.  * distribute the complete modified source code.  Modifications are to
  19.  * be distributed as patches to the released version.  Permission to
  20.  * distribute binaries produced by compiling modified sources is granted,
  21.  * provided you
  22.  *   1. distribute the corresponding source modifications from the
  23.  *    released version in the form of a patch file along with the binaries,
  24.  *   2. add special version identification to distinguish your version
  25.  *    in addition to the base release version number,
  26.  *   3. provide your name and address as the primary contact for the
  27.  *    support of your modified version, and
  28.  *   4. retain our contact information in regard to use of the base
  29.  *    software.
  30.  * Permission to distribute the released version of the source code along
  31.  * with corresponding source modifications in the form of a patch file is
  32.  * granted with same provisions 2 through 4 for binary distributions.
  33.  *
  34.  * This software is provided "as is" without express or implied warranty
  35.  * to the extent permitted by applicable law.
  36. ]*/
  37.  
  38. /*
  39.  * doc2rtf.c  -- program to convert Gnuplot .DOC format to MS windows
  40.  * help (.rtf) format.
  41.  *
  42.  * This involves stripping all lines with a leading digit or
  43.  * a leading @, #, or %.
  44.  * Modified by Maurice Castro from doc2gih.c by Thomas Williams 
  45.  *
  46.  * usage:  doc2rtf file.doc file.rtf [-d]
  47.  *
  48.  */
  49.  
  50. /* note that tables must begin in at least the second column to */
  51. /* be formatted correctly and tabs are forbidden */
  52.  
  53. #ifdef HAVE_CONFIG_H
  54. #include "config.h"
  55. #endif
  56.  
  57. #include "ansichek.h"
  58. #include "stdfn.h"
  59. #define MAX_LINE_LEN 1023
  60. #include "doc2x.h"
  61. #include "xref.h"
  62.  
  63. static boolean debug = FALSE;
  64.  
  65. void footnote __PROTO((int, char *, FILE *));
  66. void convert __PROTO((FILE *, FILE *));
  67. void process_line __PROTO((char *, FILE *));
  68.  
  69. int main(argc, argv)
  70. int argc;
  71. char **argv;
  72. {
  73.     FILE *infile;
  74.     FILE *outfile;
  75.     if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
  76.     debug = TRUE;
  77.  
  78.     if (argc != 3 && !debug) {
  79.     fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
  80.     exit(EXIT_FAILURE);
  81.     }
  82.     if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
  83.     fprintf(stderr, "%s: Can't open %s for reading\n",
  84.         argv[0], argv[1]);
  85.     exit(EXIT_FAILURE);
  86.     }
  87.     if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
  88.     fprintf(stderr, "%s: Can't open %s for writing\n",
  89.         argv[0], argv[2]);
  90.     exit(EXIT_FAILURE);
  91.     }
  92.     parse(infile);
  93.     convert(infile, outfile);
  94.     exit(EXIT_SUCCESS);
  95. }
  96.  
  97. /* generate an RTF footnote with reference char c and text s */
  98. void footnote(c, s, b)
  99. int c;
  100. char *s;
  101. FILE *b;
  102. {
  103.     fprintf(b, "%c{\\footnote %c %s}\n", c, c, s);
  104. }
  105.  
  106. void convert(a, b)
  107. FILE *a, *b;
  108. {
  109.     static char line[MAX_LINE_LEN+1];
  110.  
  111.     /* generate rtf header */
  112.     fprintf(b, "{\\rtf1\\ansi ");    /* vers 1 rtf, ansi char set */
  113.     fprintf(b, "\\deff0");    /* default font font 0 */
  114.     /* font table: font 0 proportional, font 1 fixed */
  115.     fprintf(b, "{\\fonttbl{\\f0\\fswiss Arial;}{\\f1\\fmodern Courier New;}}\n");
  116.  
  117.     /* process each line of the file */
  118.     while (get_line(line, sizeof(line), a)) {
  119.     process_line(line, b);
  120.     }
  121.  
  122.     /* close final page and generate trailer */
  123.     fprintf(b, "}{\\plain \\page}\n");
  124.     /* fprintf(b,"}\n"); *//* HBB: HACK ALERT: only without this, hc31 works */
  125.     list_free();
  126. }
  127.  
  128. void process_line(line, b)
  129. char *line;
  130. FILE *b;
  131. {
  132.     static int line_count = 0;
  133.     static char line2[MAX_LINE_LEN+1];
  134.     static int last_line;
  135.     int i;
  136.     int j;
  137.     static int startpage = 1;
  138.     char str[MAX_LINE_LEN+1];
  139.     char topic[MAX_LINE_LEN+1];
  140.     int k, l;
  141.     static int tabl = 0;
  142.     static int para = 0;
  143.     static int llpara = 0;
  144.     static int inquote = FALSE;
  145.     static int inref = FALSE;
  146.     struct LIST *klist;
  147.  
  148.     line_count++;
  149.  
  150.     i = 0;
  151.     j = 0;
  152.     while (line[i] != NUL) {
  153.     switch (line[i]) {
  154.     case '\\':
  155.     case '{':
  156.     case '}':
  157.         line2[j] = '\\';
  158.         j++;
  159.         line2[j] = line[i];
  160.         break;
  161.     case '\r':
  162.     case '\n':
  163.         break;
  164.     case '`':        /* backquotes mean boldface or link */
  165.         if (line[1] == ' ')    /* tabular line */
  166.         line2[j] = line[i];
  167.         else if ((!inref) && (!inquote)) {
  168.         k = i + 1;    /* index into current string */
  169.         l = 0;        /* index into topic string */
  170.         while ((line[k] != '`') && (line[k] != NUL))
  171.             topic[l++] = line[k++];
  172.         topic[l] = NUL;
  173.         klist = lookup(topic);
  174.         if (klist && (k = klist->line) > 0 && (k != last_line)) {
  175.             line2[j++] = '{';
  176.             line2[j++] = '\\';
  177.             line2[j++] = 'u';
  178.             line2[j++] = 'l';
  179.             line2[j++] = 'd';
  180.             line2[j++] = 'b';
  181.             line2[j] = ' ';
  182.             inref = k;
  183.         } else {
  184.             if (debug)
  185.             fprintf(stderr, "Can't make link for \042%s\042 on line %d\n", topic, line_count);
  186.             line2[j++] = '{';
  187.             line2[j++] = '\\';
  188.             line2[j++] = 'b';
  189.             line2[j] = ' ';
  190.             inquote = TRUE;
  191.         }
  192.         } else {
  193.         if (inquote && inref)
  194.             fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
  195.         if (inquote) {
  196.             line2[j] = '}';
  197.             inquote = FALSE;
  198.         }
  199.         if (inref) {
  200.             /* must be inref */
  201.             sprintf(topic, "%d", inref);
  202.             line2[j++] = '}';
  203.             line2[j++] = '{';
  204.             line2[j++] = '\\';
  205.             line2[j++] = 'v';
  206.             line2[j++] = ' ';
  207.             line2[j++] = 'l';
  208.             line2[j++] = 'o';
  209.             line2[j++] = 'c';
  210.             k = 0;
  211.             while (topic[k] != NUL)
  212.             line2[j++] = topic[k++];
  213.             line2[j] = '}';
  214.             inref = 0;
  215.         }
  216.         }
  217.         break;
  218.     default:
  219.         line2[j] = line[i];
  220.     }
  221.     i++;
  222.     j++;
  223.     line2[j] = NUL;
  224.     }
  225.  
  226.     i = 1;
  227.  
  228.     switch (line[0]) {        /* control character */
  229.     case '?':{            /* interactive help entry */
  230.         if ((line2[1] != NUL) && (line2[1] != ' '))
  231.         footnote('K', &(line2[1]), b);
  232.         break;
  233.     }
  234.     case '@':{            /* start/end table */
  235.         break;        /* ignore */
  236.     }
  237.     case '^':{            /* html link escape */
  238.         break;        /* ignore */
  239.     }
  240.     case '#':{            /* latex table entry */
  241.         break;        /* ignore */
  242.     }
  243.     case '%':{            /* troff table entry */
  244.         break;        /* ignore */
  245.     }
  246.     case '\n':            /* empty text line */
  247.     fprintf(b, "\\par\n");
  248.     llpara = para;
  249.     para = 0;
  250.     tabl = 0;
  251.     break;
  252.     case ' ':{            /* normal text line */
  253.         if ((line2[1] == NUL) || (line2[1] == '\n')) {
  254.         fprintf(b, "\\par\n");
  255.         llpara = para;
  256.         para = 0;
  257.         tabl = 0;
  258.         }
  259.         if (line2[1] == ' ') {
  260.         if (!tabl) {
  261.             fprintf(b, "\\par\n");
  262.         }
  263.         fprintf(b, "{\\pard \\plain \\f1\\fs20 ");
  264.         fprintf(b, "%s", &line2[1]);
  265.         fprintf(b, "}\\par\n");
  266.         llpara = 0;
  267.         para = 0;
  268.         tabl = 1;
  269.         } else {
  270.         if (!para) {
  271.             if (llpara)
  272.             fprintf(b, "\\par\n");    /* blank line between paragraphs */
  273.             llpara = 0;
  274.             para = 1;    /* not in para so start one */
  275.             tabl = 0;
  276.             fprintf(b, "\\pard \\plain \\qj \\fs20 \\f0 ");
  277.         }
  278.         fprintf(b, "%s \n", &line2[1]);
  279.         }
  280.         break;
  281.     }
  282.     default:{
  283.         if (isdigit((int)line[0])) {    /* start of section */
  284.         if (startpage) {    /* use new level 0 item */
  285.             refs(0, b, "\\par", NULL, "\\par{\\uldb %s}{\\v loc%d}\n");
  286.             fprintf(b, "}{\\plain \\page}\n");
  287.         } else {
  288.             refs(last_line, b, "\\par", NULL, "\\par{\\uldb %s}{\\v loc%d}\n");
  289.             fprintf(b, "}{\\plain \\page}\n");
  290.         }
  291.         para = 0;    /* not in a paragraph */
  292.         tabl = 0;
  293.         last_line = line_count;
  294.         startpage = 0;
  295.         fprintf(b, "{\n");
  296.         sprintf(str, "browse:%05d", line_count);
  297.         footnote('+', str, b);
  298.         footnote('$', &(line2[1]), b);    /* title */
  299.         fprintf(b, "{\\b \\fs24 %s}\\plain\\par\\par\n", &(line2[1]));
  300.         /* output unique ID */
  301.         sprintf(str, "loc%d", line_count);
  302.         footnote('#', str, b);
  303.         } else
  304.         fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
  305.             line[0], line_count);
  306.         break;
  307.     }
  308.     }
  309. }
  310.